package com.ktmall.ktmalluijava.service.impl;

import com.ktmall.ktmalluijava.mapper.GoodsMapper;
import com.ktmall.ktmalluijava.pojo.Goods;
import com.ktmall.ktmalluijava.service.GoodsService;
import com.ktmall.ktmalluijava.utils.Page;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ZhouShuang
 * @version 17
 * @Title
 * @date 2025/5/16 上午11:33
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsMapper goodsMapper;
    /**
     * 获取分页数据
     *
     * @param name
     * @param brandId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public Page<Goods> getPage(String name, Integer brandId, Integer pageNo, Integer pageSize) {
        Page<Goods> page = new Page<>();
        page.setPageSize(pageSize);
        page.setTotalCount(goodsMapper.count(name,brandId));
        page.setPageNo(pageNo);
        int offset = (page.getPageNo()-1)*page.getPageSize();
        page.setData(goodsMapper.select(name,brandId,offset,page.getPageSize()));
        return page;
    }

    /**
     * 添加
     *
     * @param goods
     * @return
     */
    @Override
    public int add(Goods goods) {
        return goodsMapper.add(goods);
    }

    /**
     * 商品详情
     *
     * @param id
     * @return
     */
    @Override
    public Goods detail(Integer id) {
        return goodsMapper.detail(id);
    }

    /**
     * 获取总记录数
     *
     * @param name
     * @param brandId
     * @return
     */
    @Override
    public int count(String name, Integer brandId) {
        return goodsMapper.count(name,brandId);
    }

    /**
     * 修改
     *
     * @param goods
     * @return
     */
    @Override
    public int update(Goods goods) {
        return goodsMapper.update(goods);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public int delete(Integer... id) {
        if(id.length==1){
            return goodsMapper.delete(id[0]);
        }
        int count = 0;
        for (Integer integer : id) {
            count += goodsMapper.delete(integer);
        }
        return count;
    }

    /**
     * 获取所有商品信息
     *
     * @return
     */
    @Override
    public List<Goods> getAll() {
        return goodsMapper.getAll();
    }
}
